﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Text;

namespace HYSoft.Web
{
    public partial class sensor66_list : System.Web.UI.Page
    {
        public string typename;
        public string typename11;
        public int monitorid;
        public int year1;
        public int yue1;
        public int shi1;
        public int ri1;
        public int fen1;

        public int year2;
        public int yue2;
        public int shi2;
        public int ri2;
        public int fen2;

        public string mytime1;
        public string mytime2;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(string.IsNullOrEmpty(Request.QueryString["type"]) || string.IsNullOrEmpty(Request.QueryString["areano"])))
            {
                typename = Server.UrlDecode(Request.QueryString["type"].ToString());
                typename11 = Server.UrlEncode(Request.QueryString["type"].ToString());
                monitorid = int.Parse(Request.QueryString["areano"].ToString());
                if (!IsPostBack)
                {
                    data_list();//绑定传感器（位移计）相关信息
                }
                else
                {
                    mytime1 = this.txtfrom.Value.ToString().Trim();
                    mytime2 = this.txtto.Value.ToString().Trim();
                    string[] two = mytime1.Split(' ');
                    string[] starttime = two[0].Split('-');
                    year1 = int.Parse(starttime[0].ToString());
                    yue1 = int.Parse(starttime[1].ToString());
                    ri1 = int.Parse(starttime[2].ToString());
                    string[] startshi = two[1].Split(':');
                    shi1 = int.Parse(startshi[0].ToString());
                    fen1 = int.Parse(startshi[1].ToString());

                    string[] twos = mytime2.Split(' ');
                    string[] endtime = twos[0].Split('-');
                    year2 = int.Parse(endtime[0].ToString());
                    yue2 = int.Parse(endtime[1].ToString());
                    ri2 = int.Parse(endtime[2].ToString());
                    string[] endtimes = twos[1].Split(':');
                    shi2 = int.Parse(endtimes[0].ToString());
                    fen2 = int.Parse(endtimes[1].ToString());
                }
                data_tux();
                chart_set11();
                data_show11();
            }
            else
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
        }

        protected void data_list()//根据mac地址在usr2node视图表中查询数据
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string sql = " [mac]=" + node_mac + "";
            DataSet ds = new BLL.usr2node().GetList(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }
            dt.Clear();
            dt.Dispose();
        }

        protected void data_datatime(HtmlInputText txt, int timeid, int year)
        {
            int id = timeid;
            int fen = id & 0x000000ff;
            int shi = id >> 8 & 0x000000ff;
            int ri = id >> 16 & 0x000000ff;
            int yue = id >> 24 & 0x000000ff;
            string time = year.ToString() + "-" + yue.ToString() + "-" + ri.ToString() + " " + shi.ToString() + ":" + fen.ToString() + ":00";
            txt.Value = time;
        }

        protected void data_tux()//通过传感器节点表gprsNode查询图像并显示，切仅当节点为摄像头时，设置cheryalue3的值
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string sql = " [mac]=" + int.Parse(node_mac.ToString()) + "";
            DataSet ds = new BLL.gprsNode().GetList(sql);
            DataTable dt = ds.Tables[0];
            tux = dt.Rows[0]["tux"].ToString();
            cheryalue3 = dt.Rows[0]["charvalue3"].ToString();
            dt.Clear();
            dt.Dispose();
        }
        public string tux;
        public string cheryalue3;

        protected string data_typename(object id)//传感器名
        {
            int typeid = int.Parse(id.ToString());
            string sql = " adminno=" + typeid + "";
            DataSet ds = new BLL.sensor_node().GetList(sql);
            return ds.Tables[0].Rows[0]["name"].ToString();
        }
        protected string data_addrname(object id)//所属监测点
        {
            string addrname = "";
            Model.monitor m = new BLL.monitor().GetModel(int.Parse(id.ToString()));
            if (m != null)
            {
                addrname = m.addr;
            }
            return addrname;
        }

        protected string data_count(object id)//位移计初值（增幅、位移计报警长度）设置
        {
            string count = "";
            if (Request.QueryString["type"].ToString() == "66")
            {
                if (id == DBNull.Value)
                {
                    id = 0;
                }
                float flt = float.Parse(id.ToString());
                flt /= 100;
                count = flt.ToString("0.00");
            }
            return count;
        }

        protected string data_jibie(object id)//报警级别
        {
            string name = id.ToString();
            if (name == "正常")
            {
                name = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                name = "<a style=\"color:red;\">" + name + "</a>";
            }
            return name;
        }

        protected string data_Status(object id)
        {
            string Status = "";
            if (id.ToString() == "0")
            {
                Status = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">异常</a>";
            }
            return Status;
        }

        protected string data_jingbao(object id)
        {
            string Status = "";
            switch (int.Parse(id.ToString()))
            {
                case 0:
                    Status = "<a style=\"color:green;\">正常</a>";
                    break;
                case 1:
                    Status = "<a style=\"color:red;\">报警</a>";
                    break;
                case 2:
                    Status = "<a style=\"color:orange;\">预警</a>";
                    break;
                default:
                    Status = "<a style=\"color:red;\">设备异常</a>";
                    break;
            }
            return Status;
        }

        protected string data_wendu(object id)//温度状态显示
        {
            string Status = "";
            if (id.ToString() == "正常")
            {
                Status = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">异常:高</a>";

            }
            return Status;
        }

        protected string data_dianliang(object id)//电量状态设置
        {
            string Status = "";
            if (id.ToString() == "正常")
            {
                Status = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">异常:低</a>";

            }
            return Status;
        }

        protected string data_zhuangt(object id)//传感器状态显示设置
        {
            string zhuangt = "";
            string typeid = id.ToString();
            if (typeid == "正常")
            {
                zhuangt = "<a style=\"color:green;\">正常</a>";
            }
            else if (typeid == "预警")
            {
                zhuangt = "<a style=\"color:orange;\">预警</a>";
            }
            else
            {
                zhuangt = "<a style=\"color:red;\">" + typeid + "</a>";
            }
            return zhuangt;
        }

        protected void Button1_Click(object sender, EventArgs e)//刷新按钮事件
        {
            int id = int.Parse(Request.QueryString["mac"].ToString());

            int shijian1 = (yue1 << 24) | (ri1 << 16) | (shi1 << 8) | fen1;

            int shijian2 = (yue2 << 24) | (ri2 << 16) | (shi2 << 8) | fen2;
        }

        protected void data_show11()//画图表的主框架
        {
            int id = int.Parse(Request.QueryString["mac"].ToString());
            float pjvalue = 0, maxvalue = 0, sumvalue = 0, maxy = 1, nowvalue = 0;
            string sql = " mac=" + id + "";
            DataSet ds = new BLL.gprsNodeLog().GetList(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                int time1;
                int time10;
                int year1;
                int year10;
                if (IsPostBack)
                {
                    int shijian1 = (yue1 << 24) | (ri1 << 16) | (shi1 << 8) | fen1;
                    int shijian2 = (yue2 << 24) | (ri2 << 16) | (shi2 << 8) | fen2;
                    //查询每天的最新记录
                    //select * from gprsNodeLog where [index] in (select MAX(a.[index]) from gprsNodeLog,(select [index],
                    //SUBSTRING(chartime,0,12) as 'datetime' from gprsNodeLog where mac=312) as a group by [a].[datetime])
                    StringBuilder sqlbu=new StringBuilder ();
                    sqlbu.Append("select * from gprsNodeLog where [index] in ");
                    sqlbu.Append(" (select MAX(a.[index]) from gprsNodeLog,(select [index],");
                    sqlbu.Append("SUBSTRING(chartime,0,12) as 'datetime' from gprsNodeLog where mac=312 and ");
                    sqlbu.Append("CAST(REPLACE(REPLACE (REPLACE (chartime,'年','-'),'月','-'),'日','') AS DateTime) between ");
                    sqlbu.Append(" CAST('" + mytime1 + "' AS DateTime) and CAST('" + mytime2 + "' AS DateTime)) as a group by [a].[datetime])");
                    string sql1 = sqlbu.ToString();
                    // sql = "mac=" + id + " and CAST(REPLACE(REPLACE (REPLACE (chartime,'年','-'),'月','-'),'日','') AS DateTime) between  CAST('" + mytime1 + "' AS DateTime) and CAST('" + mytime2 + "' AS DateTime)";
                    dt = new BLL.bpcddata().GetList("all",sql1).Tables[0];
                   // dt = new BLL.gprsNodeLog().GetList(sql).Tables[0];
                    time10 = int.Parse(dt.Rows[dt.Rows.Count - 1]["time"].ToString());
                    time1 = int.Parse(dt.Rows[0]["time"].ToString());
                    year10 = int.Parse(dt.Rows[dt.Rows.Count - 1]["year"].ToString());
                    year1 = int.Parse(dt.Rows[0]["year"].ToString());
                }
                else
                {
                    dt = new BLL.gprsNodeLog().GetList(10, sql, "[index]").Tables[0];
                    time10 = int.Parse(dt.Rows[dt.Rows.Count - 1]["time"].ToString());
                    time1 = int.Parse(dt.Rows[0]["time"].ToString());
                    year10 = int.Parse(dt.Rows[dt.Rows.Count - 1]["year"].ToString());
                    year1 = int.Parse(dt.Rows[0]["year"].ToString());
                }
                data_datatime(txtfrom, time1, year1);
                data_datatime(txtto, time10, year10);
            }
            DataView dv = new DataView(dt);
            Chart1.Series["Series1"].Points.DataBindXY(dv, "chartime", dv, "intvalue");
           // Chart1.Width = dt.Rows.Count * 100 >= 2000 ? 800 : dt.Rows.Count * 100;
           // Chart1.Width = (dt.Rows.Count * 100 + 200) >= 700 ? dt.Rows.Count * 100 + 200 : 700;
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                nowvalue = float.Parse(dt.Rows[i]["intvalue"].ToString()) / 100;
                sumvalue = sumvalue + nowvalue;
                if (nowvalue > maxvalue)
                    maxvalue = nowvalue;
            }
            pjvalue = sumvalue / dt.Rows.Count;
            pjvalue = pjvalue * 2;
            if (pjvalue < maxvalue)
                maxy = maxvalue * (float)1.1;
            else
                maxy = pjvalue;

            Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = maxy;
            dt.Clear();
            dt.Dispose();
        }

        protected void chart_set11()//画图表的线条位置和背景色等属性的设置
        {
            Chart1.Series["Series1"].Points.Clear();
            Chart1.Series["Series1"].IsValueShownAsLabel = true;
            Chart1.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
            Chart1.Series["Series1"].MarkerSize = 10;
            Chart1.Series["Series1"].MarkerBorderWidth = 2;
            Chart1.Series["Series1"].Color = Color.Red;
            Chart1.Series["Series1"].BorderWidth = 2;
            Chart1.Series["Series1"].ShadowOffset = 1;
            //Chart1.Series["Series1"].ChartType = SeriesChartType.Spline;

            // Set axis title
            Chart1.ChartAreas["ChartArea1"].AxisX.Title = "记录时间";
            Chart1.ChartAreas["ChartArea1"].AxisY.Title = "位移值(mm)";

            // Set Title font
            Chart1.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("Times New Roman", 12, FontStyle.Bold);
            Chart1.ChartAreas["ChartArea1"].AxisY.TitleFont = new Font("Times New Roman", 12, FontStyle.Bold);


            Chart1.ChartAreas["ChartArea1"].ShadowColor = Color.Transparent;
            Chart1.ChartAreas["ChartArea1"].BackColor = Color.Azure;
            Chart1.ChartAreas["ChartArea1"].BackGradientStyle = GradientStyle.TopBottom;
            Chart1.ChartAreas["ChartArea1"].BackSecondaryColor = Color.White;

            Chart1.ChartAreas["ChartArea1"].AxisX.LineWidth = 2;
            Chart1.ChartAreas["ChartArea1"].AxisY.LineWidth = 2;

            Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;

            Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.LightGray;
            Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.LightGray;

            Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Interval = 0.5;

            Chart1.Titles[0].Text = "位移值曲线";
            Chart1.Titles[0].Font = new Font("Times New Roman", 16, FontStyle.Bold);
        }

        protected void Chart1_Customize(object sender, EventArgs e)//鼠标悬浮提示
        {
            int id = int.Parse(Request.QueryString["mac"].ToString());
            StringBuilder sqlbu = new StringBuilder();
            sqlbu.Append("select * from gprsNodeLog where [index] in ");
            sqlbu.Append(" (select MAX(a.[index]) from gprsNodeLog,(select [index],");
            sqlbu.Append("SUBSTRING(chartime,0,12) as 'datetime' from gprsNodeLog where mac=312 and ");
            sqlbu.Append("CAST(REPLACE(REPLACE (REPLACE (chartime,'年','-'),'月','-'),'日','') AS DateTime) between ");
            sqlbu.Append(" CAST('" + mytime1 + "' AS DateTime) and CAST('" + mytime2 + "' AS DateTime)) as a group by [a].[datetime])");
            string sql1 = sqlbu.ToString();
                    
            string sql = " mac=" + id + "";
            DataSet ds = new BLL.gprsNodeLog().GetList(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataSet ds1;
                if (IsPostBack)
                {
                    int shijian1 = (yue1 << 24) | (ri1 << 16) | (shi1 << 8) | fen1;
                    int shijian2 = (yue2 << 24) | (ri2 << 16) | (shi2 << 8) | fen2;
                    //sql += " and CAST(REPLACE(REPLACE (REPLACE (chartime,'年','-'),'月','-'),'日','') AS DateTime) between  CAST('" + mytime1 + "' AS DateTime) and CAST('" + mytime2 + "' AS DateTime)";
                    //ds1 = new BLL.gprsNodeLog().GetList(sql);
                    ds1 = new BLL.bpcddata().GetList("all",sql1);

                }
                else
                {
                    ds1 = new BLL.gprsNodeLog().GetList(10, sql, "[index] desc");
                }
                dt = ds1.Tables[0];
            }

            for (int i = 0; i < Chart1.Series["Series1"].Points.Count; i++)
            {
                string rzsj = "日志时间:" + dt.Rows[i]["chartime"].ToString();
                string wyz = "位移值:" + (float.Parse(dt.Rows[i]["intvalue"].ToString()) / 100).ToString("0.00") + "mm";
                string wdzt = "";
                if (dt.Rows[i]["wendu"].ToString() == "1")
                {
                    wdzt = "温度状态:正常";
                }
                else
                {
                    wdzt = "温度状态:异常高";

                }
                string dczt = "";
                if (dt.Rows[i]["dianchi"].ToString() == "1")
                {
                    dczt = "电池状态:正常";
                }
                else
                {
                    dczt = "电池状态:异常";

                }
                string gdms = "";
                if (dt.Rows[i]["gongdian"].ToString() == "1")
                {
                    gdms = "供电模式:电池";
                }
                else
                {
                    gdms = "供电模式:非电池";

                }
                string dlzt = "";
                if (dt.Rows[i]["dianliang"].ToString() == "1")
                {
                    dlzt = "电量状态:正常";
                }
                else
                {
                    dlzt = "供电模式:异常低";

                }
                string cgqzt = "";
                int typeid = int.Parse(dt.Rows[i]["chuanganqi"].ToString());
                if (typeid == 0)
                {
                    cgqzt = "传感器状态:正常";
                }
                else
                {
                    typeid = typeid & 0x80;
                    if (typeid == 0x80)
                    {
                        cgqzt = "传感器状态:报警";
                    }
                    else
                    {
                        cgqzt = "传感器状态:预警";
                    }
                }

                string jbjb = "";
                switch (int.Parse(dt.Rows[i]["jingbao"].ToString()))
                {
                    case 0:
                        jbjb = "警报级别:正常";
                        break;
                    case 1:
                        jbjb = "警报级别:异常";
                        break;
                    case 2:
                        jbjb = "警报级别:预警";
                        break;
                    default:
                        jbjb = "警报级别:设备异常";
                        break;
                }

                string dxtzbz = "";
                switch (int.Parse(dt.Rows[i]["jingbao"].ToString()))
                {
                    case 0:
                        break;
                    default:
                        {
                            if (dt.Rows[i]["sendmsg"].ToString() == "0")
                            {
                                dxtzbz = "短信通知标志:已短信通知";
                            }
                            else
                            {
                                dxtzbz = "短信通知标志:未短信通知";
                            }
                        }
                        break;
                }
                Chart1.Series["Series1"].Points[i].ToolTip = rzsj + "\n" + wyz + "\n" + wdzt + "\n" + dczt + "\n" + gdms + "\n" + dlzt + "\n" + cgqzt + "\n" + jbjb + "\n" + dxtzbz;
            }
            dt.Clear();
            dt.Dispose();
        }
        protected void Chart1_PreRender(object sender, EventArgs e)
        {
            for (int i = 0; i < Chart1.Series["Series1"].Points.Count; i++)
            {
                Chart1.Series["Series1"].Points[i].YValues[0] = Chart1.Series["Series1"].Points[i].YValues[0] / 100;
            }
        }
    }
}